//
//  NSString+PLDecimalNumberCalc.m
//  PlatformCommon
//
//  Created by KimmanYip on 2018/6/11.
//

#import "NSString+PLDecimalNumberCalc.h"

@implementation NSString(PLDecimalNumberCalc)

/** 加 */
- (NSString *)pl_stringByAdding:(NSString *)string
{
    NSDecimalNumber *addendNumber = [NSDecimalNumber decimalNumberWithString:self];
    NSDecimalNumber *secondNumber = [NSDecimalNumber decimalNumberWithString:string];
    NSDecimalNumber *result = [secondNumber decimalNumberByAdding:addendNumber];
    return [result stringValue];
}

/** 减 */
- (NSString *)pl_stringBySubtracting:(NSString *)string
{
    NSDecimalNumber *minuendNumber = [NSDecimalNumber decimalNumberWithString:self];
    NSDecimalNumber *subtrahendNumber = [NSDecimalNumber decimalNumberWithString:string];
    NSDecimalNumber *result = [minuendNumber decimalNumberBySubtracting:subtrahendNumber];
    return [result stringValue];
}

/** 乘 **/
- (NSString *)pl_stringByMultiplyingBy:(NSString *)string
{
    NSDecimalNumber *multiplicandNumber = [NSDecimalNumber decimalNumberWithString:self];
    NSDecimalNumber *multiplierNumber = [NSDecimalNumber decimalNumberWithString:string];
    NSDecimalNumber *result = [multiplicandNumber decimalNumberByMultiplyingBy:multiplierNumber];
    return [result stringValue];
}

/** 除 */
- (NSString *)pl_stringByDividingBy:(NSString *)string
{
    NSDecimalNumber *dividendNumber = [NSDecimalNumber decimalNumberWithString:self];
    NSDecimalNumber *divisorNumber = [NSDecimalNumber decimalNumberWithString:string];
    NSDecimalNumber *result = [dividendNumber decimalNumberByDividingBy:divisorNumber];
    return [result stringValue];
}

/** 转换 */
+ (NSString *)pl_decimalNumberWithDouble:(double)conversionValue
{
    
    NSString *doubleString        = [NSString stringWithFormat:@"%lf", conversionValue];
    
    NSDecimalNumber *decNumber    = [NSDecimalNumber decimalNumberWithString:doubleString];
    
    return [decNumber stringValue];
    
}

/** 比较大小 */
- (NSComparisonResult)pl_compareWith:(NSString *)value {
    NSDecimalNumber *firstNumber = [NSDecimalNumber decimalNumberWithString:self];
    NSDecimalNumber *secondNumber = [NSDecimalNumber decimalNumberWithString:value];
    return [firstNumber compare:secondNumber];
}

- (NSString *)pl_roundingWith2Scale {
    
    NSString *format = nil;
    if (self && self.length > 0) {
        NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithString:self ];
        //保留两位并四舍五入
        NSDecimalNumberHandler *roundUp = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:2 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:YES];
        number = [number decimalNumberByRoundingAccordingToBehavior:roundUp];
        format = [NSString stringWithFormat:@"%@", number];
        
        // 补0
        NSRange range = [format rangeOfString:@"."];
        if (range.location != NSNotFound) {
            NSString *sub = [format substringFromIndex:range.location+1];
            if (sub.length == 1) {
                format = [format stringByAppendingString:@"0"];
            }
        } else {
            format = [format stringByAppendingString:@".00"];
        }
    } else {
        format = @"0.00";
    }
    
    return format;
}

@end
